package images_api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"zswei.xyz/manager/global"
	"zswei.xyz/manager/models"
	"zswei.xyz/manager/models/res"
	"zswei.xyz/manager/plugins/qiniu"
)

// ImageRemoveView 删除文件【批量】
//
//	@Tags			文件管理
//	@Summary		删除文件【批量】
//	@Description	删除文件【批量】接口
//	@Param			data	body	models.RemoveRequest	true	"删除文件参数"
//	@Router			/v2/image/upload [delete]
//	@Produce		json
//	@Success		200	{object}	res.Response{}
func (ImagesApi) ImageRemoveView(c *gin.Context) {
	var cr models.RemoveRequest

	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithCode(res.ArgumentError, c)
		return
	}

	var imageList []models.BannerModel
	count := global.DB.Find(&imageList, cr.IDList).RowsAffected
	if count == 0 {
		res.FailWithDesc("未找到删除项", c)
		return
	}
	var delQiNiuList []string
	for _, key := range imageList {
		// 是七牛云的才删除
		if key.ImageType == 2 {
			delQiNiuList = append(delQiNiuList, key.Path)
		}
	}
	// 如果是七牛云的才执行这个删除
	var qiNiuMsg string
	if len(delQiNiuList) != 0 {
		img := qiniu.DelQiNiuImg(global.Config.QiNiu.Bucket, delQiNiuList)
		qiNiuMsg = "-" + img
	}
	global.DB.Delete(&imageList)
	res.OkWithDesc(fmt.Sprintf("成功删除 %d 个文件-本地%d个%s", count, count-int64(len(delQiNiuList)), qiNiuMsg), c)
}
